package com.youyiwen.Service.impl;

import com.youyiwen.Bean.Message;
import com.youyiwen.Bean.User;
import com.youyiwen.Mapper.MessageMapper;
import com.youyiwen.Mapper.UserMapper;
import com.youyiwen.Service.MessageService;
import com.youyiwen.dto.MessageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhaoyang
 * @Date: 2021/04/16
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<MessageDTO> messagesList(String sender,String receiver,String forUser) {
        List<MessageDTO> list = new ArrayList<>();
        List<Message> messagesList = messageMapper.selectMessages(sender,receiver,forUser);
        for (Message message : messagesList) {
            MessageDTO messageDTO = new MessageDTO();
            User senderUser = userMapper.selectUserByName(message.getSender());
            User receiveUser = userMapper.selectUserByName(message.getReceiver());
            messageDTO.setMessage(message);
            messageDTO.setSender(senderUser);
            messageDTO.setReceiver(receiveUser);
            list.add(messageDTO);
        }
        return list;
    }
}
